package com.example.mybatisplusstudy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplusstudy.domain.User;
import com.example.mybatisplusstudy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RequestMapping("/users")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public boolean addUser(@RequestBody User user){
        return userService.save(user);
    }

    @PutMapping("/update")
    public boolean updateUserById(@RequestBody User user){
        return userService.saveOrUpdate(user);
    }

    @GetMapping("/{user_name}")
    public User getUserByUserName(@PathVariable("user_name") String username){
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("user_name",username);
        return userService.getOne(qw);
    }

    @DeleteMapping("/delete/{username}")
    public Boolean deleteUserByName(@PathVariable String username){
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_name",username);
        return userService.removeByMap(map);
    }
}
